let app = getApp();

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    onBookshelfBottomCount: {
      type: Number,
      value: 1,
      observer: 'onBookshelfBottomMethod'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    typeList: [
      { id: 0, name: 'ھەممىسى' },
      { id: 5, name: 'يېڭىلىنىۋاتقان' },
      { id: 4, name: 'ئوقىۋاتقان' },
      { id: 3, name: 'تۈرى' },
      { id: 2, name: 'ھەققىلىق' },
      { id: 1, name: 'ھەقسىز' },
    ],

    // 轮播图内容
    swiperList: [
      { id: 0, name: 'ھەممىسى' },
      { id: 1, name: 'ھەقسىز' },
      { id: 2, name: 'ھەققىلىق' },
      { id: 3, name: 'تۈرى' },
      { id: 4, name: 'ئوقىۋاتقان' },
      { id: 5, name: 'يېڭىلىنىۋاتقان' },
    ],

    // 全部
    allBook: [],
    // 更新
    genxin: [
      // { id: 0, year: 2023, books: [
      //   { id: 1, name: 'سىناق تىما بۇ', type: 1, endDate: '2023.8', coverUrl: 'https://cdn.weread.qq.com/weread/cover/96/cpPlatform_kmqPY1boCDVyMxq2AvPdCY/t6_cpPlatform_kmqPY1boCDVyMxq2AvPdCY.jpg' },
      //   { id: 2, name: 'سىناق تىما بۇ', type: 1, endDate: '2022.3', coverUrl: 'https://cdn.weread.qq.com/weread/cover/42/cpplatform_hv5zqtwgtgpezherv2kfcn/t6_cpplatform_hv5zqtwgtgpezherv2kfcn1731659325.jpg' },
      //   { id: 3, name: 'سىناق تىما بۇ', type: 1, endDate: '2012.12', coverUrl: 'https://wfqqreader-1252317822.image.myqcloud.com/cover/728/815728/t6_815728.jpg' },
      //   { id: 4, name: 'سىناق تىما بۇ', type: 2, endDate: '2015.3', coverUrl: 'https://wehear-1258476243.file.myqcloud.com/hemera/cover/809/ec0/t6_0755db803ad545c4f9a24087d3.jpg' },
      // ] },
      // { id: 1, year: 2022, books: [
      //   { id: 1, name: 'سىناق تىما بۇ', type: 1, endDate: '2012.12', coverUrl: 'https://wfqqreader-1252317822.image.myqcloud.com/cover/728/815728/t6_815728.jpg' },
      // ] },
      // { id: 2, year: 2021, books: [
      //   { id: 1, name: 'سىناق تىما بۇ', type: 1, endDate: '2012.12', coverUrl: 'https://wfqqreader-1252317822.image.myqcloud.com/cover/728/815728/t6_815728.jpg' },
      //   { id: 2, name: 'سىناق تىما بۇ', type: 1, endDate: '2012.12', coverUrl: 'https://wfqqreader-1252317822.image.myqcloud.com/cover/728/815728/t6_815728.jpg' },
      // ] },
      // { id: 3, year: 2020, books: [
      //   { id: 1, name: 'سىناق تىما بۇ', type: 1, endDate: '2012.12', coverUrl: 'https://wfqqreader-1252317822.image.myqcloud.com/cover/728/815728/t6_815728.jpg' },
      //   { id: 2, name: 'سىناق تىما بۇ', type: 1, endDate: '2012.12', coverUrl: 'https://wfqqreader-1252317822.image.myqcloud.com/cover/728/815728/t6_815728.jpg' },
      // ] },
    ],
    // 进度
    jindu: [],
    // 分类
    fenlei: [],
    // 付费
    fufei: [],
    // 免费
    mianfei: [],
    isLoading: true, // 是否加载完成
    activeId: 0,
    morenH: '', // 默认高度
    goHeight: 'morenH', // 获取高度对象
    notificationShow: false,
    notificationMessage: 'سىستىمغا كىردىڭىز !',
    pageNo: 1,
    pageSize: 30,
    totalPage: 1,
    type: -1,
    user_id: 1,

    isLoginMask: false, 
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击类型
    onType(item) {
      this.setData({ activeId: item.currentTarget.dataset.item });
    },

    // swiper 滑动
    onChange(e) {
      this.setData({ activeId: e.detail.current });
      
      switch (e.detail.current) {
        case 0:
          this.setData({ goHeight: 'morenH', type: -1 })
          break;
        case 1:
          this.setData({ goHeight: 'mianfei', type: 4 })
          break;
        case 2:
          this.setData({ goHeight: 'fufei', type: 3 })
          break;
        case 3:
          this.setData({ goHeight: 'fenlei', type: 2 })
          break;
        case 4:
          this.setData({ goHeight: 'jindu', type: 1 })
          break;
        case 5:
          this.setData({ goHeight: 'genxinH', type: 0 })
          break;
      
        default:
          break;
      }
      this.getSwiperH(); // 计算高
      this.changeType(); // 监听类型变化
    },

    // 获取 swiper 高度
    getSwiperH() {
      const query = wx.createSelectorQuery().in(this);
      let that = this;
      let gao = `#${this.data.goHeight}`; // morenH
      query.select(gao).boundingClientRect(function (res) {
        if (res) {
          that.setData({ morenH: that.data.allBook.length / 3 * 310 + 310 })
        } else {
          console.log('未找到对应的 view 元素');
        }
      }).exec();
    },

    // 点击书
    onBook(item) {
      let postId = item.currentTarget.dataset.item.id;
      wx.navigateTo({
        url: `/pages/audioList/audioList?postId=${postId}`,
      })
    },

    // 获取收藏书籍
    getFavoritesBooks() {
      let userInfo = wx.getStorageSync('userInfo');
      if (!userInfo) return this.setData({ isLoginMask: true });
      
      let pageNo = this.data.pageNo;
      let pageSize = this.data.pageSize;
      let type = this.data.type;
      let user_id = this.data.user_id;
      let that = this;
      wx.request({
        method: 'POST',
        url: `${app.globalData.API_URL}/getFavorites`,
        header: app.header(),
        data: { user_id, type, pageNo, pageSize },
        success(res) {
          if(res.statusCode == 200) {
            let postData = res.data.data
            that.setData({ allBook: that.data.allBook.concat(postData), totalPage: res.data.totalPage })

            that.getSwiperH(); // 计算高度
            that.LoadingFinish(); // 完成加载
          }
        }
      })
    },

    // 监听类型变化
    changeType() {
      this.getFavoritesBooks();
      console.log("更改监听 ~", this.data.activeId);
    },

    // 检查用户登录
    isLogin() {
      let userInfo = wx.getStorageSync('userInfo')
      if (!userInfo) {
        this.setData({ isLoginMask: true })
      } else {
        this.setData({ user_id: userInfo.user.id })
      }
    },

    // 隐藏登录提示组件
    changeLoginMask(e) {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({ isLoginMask: e.detail.isLoginMask, user_id: userInfo.user.id, notificationShow: true });
      this.getFavoritesBooks();
    },

    // 加载完成
    LoadingFinish() {
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 300)
    },

    // 分页
    noMoreMethod(pull) {
      if(this.data.pageNo == this.data.totalPage) return
      let pageNo = this.data.pageNo += 1
      this.setData({pageNo : pageNo})
      this.getSwiperH()
      this.getFavoritesBooks();
    },

    // 获取高实现翻页
    onBookshelfBottomMethod(number) {
      this.noMoreMethod(false)
    },

  },
  lifetimes: {
    created () {
      this.isLogin();
      this.getFavoritesBooks();
    }
  },
})